package org.popupcommunication.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.popupcommunication.exceptions.EventNotFoundException;
import org.popupcommunication.model.Agenda;
import org.popupcommunication.model.AgendaGroupe;
import org.popupcommunication.model.AgendaPerso;
import org.popupcommunication.model.User;
import org.popupcommunication.service.AgendaGroupeManager;
import org.popupcommunication.service.AgendaPersoManager;
import org.popupcommunication.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("agenda/")
public class AgendaController {
	@Autowired UserManager userService;	
	@Autowired AgendaGroupeManager agendaGroupeService;
	@Autowired AgendaPersoManager agendaPersoService;
	
	private static final String listEvents ="agenda/list";	
	private static final String viewEvents ="agenda/fiche";	
	private static final String viewEventsPerso ="agenda/fichePerso";
	
	@RequestMapping(value="liste",method = RequestMethod.GET)	
	public String viewList (Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);		
	    List<Agenda> currentsEvents = userService.getCurrentsEventsUsers(toto);
	    model.addAttribute("currentAgendaGroupes", currentsEvents);
	return listEvents;
	}	
	
	@RequestMapping(value = "/evenement/{idSujet}", method = RequestMethod.GET)
	public String viewEvent(HttpServletRequest request,Model model,@PathVariable("idSujet") Integer id) throws EventNotFoundException{
	AgendaGroupe ag = agendaGroupeService.findById(id)	;
	if (ag == null){
		throw new EventNotFoundException();
	}
	 String path = request.getContextPath()+"/resources/upload/groupe/event/"+ag.getImg();
	model.addAttribute("event", ag);
    model.addAttribute("image", path);	
	return viewEvents;	
	}
	@RequestMapping(value = "/evenement/perso/{idSujet}", method = RequestMethod.GET)
	public String viewEventPerso(HttpServletRequest request,Model model,@PathVariable("idSujet") Integer id) throws EventNotFoundException{
	AgendaPerso ap = agendaPersoService.findById(id)	;
	if (ap == null){
		throw new EventNotFoundException();
	}
	 String path = request.getContextPath()+"/resources/upload/groupe/event/"+ap.getImg();
	model.addAttribute("event", ap);
    model.addAttribute("image", path);	
	return viewEventsPerso;	
	}
	@RequestMapping(value="liste/perso",method = RequestMethod.GET)	
	public String viewListPerso (Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);		
	    List<Agenda> currentsEvents = userService.getCurrentsEventsUsers(toto);
	    model.addAttribute("currentAgendaGroupes", currentsEvents);
	return listEvents;
	}		
	
	@ExceptionHandler(EventNotFoundException.class)
	public ModelAndView handleCustomException(EventNotFoundException ex) {

		ModelAndView model = new ModelAndView("errors/custom_error");
		model.addObject("exception", ex);
		return model;
 
	}
}
